package com.wshl.aijiuunion.view.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.wshl.aijiuunion.R;


/**
 * Created by Administrator on 2018/11/21.
 * 加载动画
 */

public class Loadingdialog {

    public static Dialog dialog;
    private static AlertDialog dialog1;

    public static void showAlert(Context context) {

        AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.Dialog);
        dialog1 = builder.create();
        // View dialogView = View.inflate(context, R.layout.dialog_loadingview, null);
        View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_loadingview, null);
        //设置对话框布局
        dialog1.setView(dialogView);
       // dialog1.setCanceledOnTouchOutside(false);
        //dialog1.show();
        if (!((Activity) context).isFinishing()) {
            if (!dialog1.isShowing()) {
                dialog1.show();
            }
        }

    }

    public static void showAlert(Context context, String content) {

        AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.Dialog);
        dialog1 = builder.create();
        // View dialogView = View.inflate(context, R.layout.dialog_loadingview, null);
        View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_loadingview, null);
        TextView tvLoading = dialogView.findViewById(R.id.tv_loading);
        if (!TextUtils.isEmpty(content)) {
            tvLoading.setText(content);
        }
        //设置对话框布局
        dialog1.setView(dialogView);
        dialog1.setCanceledOnTouchOutside(false);//点击空白区域,dialog不消失,点击返回键可以消失
        //dialog1.show();
        if (!((Activity) context).isFinishing()) {
            if (!dialog1.isShowing()) {
                dialog1.show();
            }
        }

    }

    public static void show(Context context, String content) {


        if (dialog == null) {
            dialog = new Dialog(context, R.style.Dialog);
            dialog.setCanceledOnTouchOutside(false);
            dialog.setCancelable(false);
            View view = View.inflate(context, R.layout.dialog_loadingview, null);
            TextView tvLoading = view.findViewById(R.id.tv_loading);
            if (!TextUtils.isEmpty(content)) {
                tvLoading.setText(content);
            }
            dialog.setContentView(view);
//            dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
//                @Override
//                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
//                    if (keyCode == KeyEvent.KEYCODE_BACK) {
//                        //点击返回键时,dialog消失
//                        dismiss();
//                        return true;
//                    }
//                    return false;
//                }
//            });

            if (!((Activity) context).isFinishing()) {
                if (!dialog.isShowing()) {
                    dialog.show();
                }
            }

        } else {

            if (!((Activity) context).isFinishing()) {
                if (!dialog.isShowing()) {
                    dialog.show();
                }
            }

        }
    }

    public static void show(Context context) {

        if (dialog == null) {

            dialog = new Dialog(context, R.style.Dialog);
            dialog.setCancelable(true);
            dialog.setCanceledOnTouchOutside(true);
            dialog.setContentView(R.layout.dialog_loadingview);
//            dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
//                @Override
//                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
//                    if (keyCode == KeyEvent.KEYCODE_BACK) {
//                        //点击返回键时,dialog消失
//                        dismiss();
//                        return true;
//                    }
//                    return false;
//                }
//            });

            if (!((Activity) context).isFinishing()) {
                if (!dialog.isShowing()) {
                    dialog.show();
                }
            }

        } else {

            LogUtils.i("刷新dialog");
            if (!((Activity) context).isFinishing()) {
                if (!dialog.isShowing()) {
                    dialog.show();
                }
            }

        }
    }

    public static void dismiss() {

//        if (dialog != null && dialog.isShowing()) {
//            dialog.dismiss();
//            dialog = null;
//        }
//        if (dialog != null && !dialog.isShowing()) {
//            // dialog.dismiss();
//            dialog = null;
//        }


        if (dialog1 != null && dialog1.isShowing()) {
            dialog1.dismiss();
            dialog1 = null;
        }
        if (dialog1 != null && !dialog1.isShowing()) {
            // dialog.dismiss();
            dialog1 = null;
        }

    }


}
